import { ParseClass } from './parse';
import V2Game from './v2/game';
import V1Game from './v1/game';
import { IGame } from './Common';

let GameVersionMap = {
    "1": V1Game,
    "2": V2Game,
}


function trimStr(str: string) {
    str = str.replaceAll(/[\n|\r\n]/g, "");
    str = str.replaceAll(/&nbsp;/g, " ")
    return str;
}

window.addEventListener('load', function () {
    var pList: HTMLElement[];
    //@ts-ignore
    pList = document.querySelectorAll('div,p');

    // var iapEl: HTMLElement;
    var model = 0; // 0:无  1: 一行模式 2: 多行模式
    var el: HTMLElement;
    var iapText: string;
    var text = "";
    var i;
    // var game : Game;
    var includeDoms : HTMLElement[] = [];
    for (i = 0; i < pList.length; i++) {
        const item = pList[i];
        if (item.childElementCount == 0) {
            text = item.textContent || '';
            if(text.indexOf("[:IAP") != -1) {
                // console.log("在节点中找到",item);
                text = trimStr(text);
                el = item;
                if(/\[:IAP(.*?)\[\/:IAP\]/.test(text)) {
                    //一行模式
                    model = 1;
                } else if(el!.parentElement) {
                    model = 2;
                }
                else {
                    model = 0;
                }
                break;
            }
        }
    }
    // console.log("寻找模式", model);

    if(model == 1) {
        includeDoms.push(el!);
    }

    console.log("model", model);
    var startIndex = -1;
    if(model == 2) {
        var children = el!.parentElement!.children;
        text = '';
        model = 0;
        for (i = 0; i < children.length; i++) {
            const element = <HTMLElement>children[i];
            var te1 = element.textContent || ''
            if(startIndex == -1 &&  te1.indexOf("[:IAP")  != -1) {
                startIndex = i;
            }
            if(startIndex != -1) {
                text += te1;
                includeDoms.push(element);
                if(text.indexOf("[/:IAP]") != -1) {
                    model = 2;
                    break;
                }
            }
        }
    }


    if(model != 0) {
        text = trimStr(text)
        var res = /\[:IAP(.*?)\[\/:IAP\]/.exec(text);
        if(res) {
            iapText = res[0]
        }
        else {
            model = 0;
        }
    }
    
    // console.log("寻找模式第二次", model)
    // console.log("字符串", text)
    // console.log("节点", el)
    var game : IGame;
    if(model != 0) {
        var parseClass = new ParseClass(iapText!);
        if(parseClass.isMatchIap()) {
            var node = parseClass.parse();
            console.log("Iap解析后数据", node);
            if(node.getAttr("version") == "2") {
                game = new GameVersionMap["2"]()
                game.init(node);
                //@ts-ignore
                window.iapGame = game;
            }
            else {
                model = 0;
            }
        }
    }
    console.log(includeDoms);
    if(model != 0 ) {
        
        for (let i = 0; i < includeDoms.length; i++) {
            if(i == 0) {
                includeDoms[0].innerText = "";
                includeDoms[0].appendChild(game!.el);
            }else {
                includeDoms[i].remove();
            }
        }
        game!.start();
    }

});



